package cn.yj.relay.core.service.impl;

import cn.yj.relay.common.utils.DateTimeUtils;
import cn.yj.relay.common.utils.StringUtils;
import cn.yj.relay.core.entity.Emp;
import cn.yj.relay.core.entity.Task;
import cn.yj.relay.core.mapper.EmpMapper;
import cn.yj.relay.core.mapper.TaskMapper;
import cn.yj.relay.core.service.IEmpService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 永健
 * @since 2020-03-03 17:41
 */
@Service
@Transactional
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements IEmpService
{

    @Autowired
    TaskMapper taskMapper;

    @Override
    public Page<Emp> findList(Map<String, Object> map, Page<Emp> page)
    {
        List<Emp> list = baseMapper.findList(map, page);
        list.forEach(emp -> {
            Integer empId = emp.getId();
            Integer count = taskMapper.selectCount(new QueryWrapper<Task>().lambda().eq(Task::getEmpId, empId));
            emp.setOrderCount(count);
            emp.setAllMoney(taskMapper.selectSumAllMoney(empId));
            emp.setRoyaltyMoney(taskMapper.selectSumRoyaltyMoney(empId));
        });
        return page.setRecords(list);
    }

    @Override
    public Emp getByToken(String token)
    {
        return getOne(new QueryWrapper<Emp>().lambda().eq(Emp::getToken, token));
    }

    @Override
    public Emp login(String count, String password, String token)
    {
        Emp emp = getOne(new QueryWrapper<Emp>()
                .lambda()
                .eq(Emp::getPhone, count)
                .eq(Emp::getPassword, password));
        if (StringUtils.isNotNull(emp))
        {
            emp.setToken(token);
            updateById(emp);
        }
        return emp;
    }




}
